﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarsModel;
using CarsDal;
using Lib.Data;

namespace CarsBll
{
    public class GoodsBll
    {
        GoodsDal dal=new GoodsDal();
        CarsTypeDal typedal = new CarsTypeDal();
        /// <summary>
        /// 卖家上架商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int GoodsADD(Goods model)
        {
            int i = dal.AddGoods(model);
            return i;
        }

        /// <summary>
        /// 管理员修改商品信息并发布
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateGoods(Goods model)
        {
            //管理员完善信息，上架商品装态修改为审核通过
            model.GoodsState = 1;

            int TypeID = model.TypeID;
            
            if (TypeID == null || TypeID == 0)
            {
                int a = typedal.CarsTypeADD(model.GoodsType,0);
                if (a > 0)
                {
                    List<CarsType> list = typedal.CarsTypeList();
                    CarsType CarsType = list.Where(p => p.TypeName == model.GoodsType).FirstOrDefault();
                    typedal.CarsTypeADD(model.GoodsSet, CarsType.TypeID);
                }
            }
            int i = dal.GoodsUpdate(model);
            return i;
        }

        /// <summary>
        /// 快速搜索商品信息
        /// </summary>
        /// <param name="GoodsPai">车牌</param>
        /// <param name="MinPrice">最低价</param>
        /// <param name="MaxPrice">最高价</param>
        /// <param name="GoodsMileage">行驶里程</param>
        /// <param name="CarsModel">车型</param>
        /// <returns>商品信息的集合</returns>
        public List<Goods> GoodsList(string GoodsType, decimal MinPrice, decimal MaxPrice, int GoodsMileage, int CarsModel)
        {
            List<Goods> GoodsList = dal.GoodsList();
            List<Goods> list = GoodsList.Where(p => p.GoodsType.Contains(GoodsType) && p.GoodsPrice <= MaxPrice && p.GoodsPrice >= MinPrice && p.GoodsMileage <= GoodsMileage && p.CarsModelsID == CarsModel).ToList();
            return list;
        }

        /// <summary>
        /// 卖家查询自己上架的商品信息
        /// </summary>
        /// <param name="UsersID">当前用户的ID</param>
        /// <returns>商品集合</returns>
        public List<Goods> GoodsList(int UsersID)
        {
            List<Goods> GoodsList = dal.GoodsList();
            List<Goods> list = GoodsList.Where(p => p.UsersID == UsersID).ToList();
            return list;
        }
        /// <summary>
        /// 查询车型信息
        /// </summary>
        /// <param name="CarsModelID">车型主键ID</param>
        /// <returns></returns>
        public List<Goods> GoodsListModel(int CarsModelID)
        {
            List<Goods> GoodsList = dal.GoodsList();
            List<Goods> list = GoodsList.Where(p => p.CarsModelsID == CarsModelID).ToList();
            return list;
        }

        /// <summary>
        /// 商品数据总条数
        /// </summary>
        /// <returns></returns>
        public int GoodsCount()
        {
            List<Goods> ListGoods = dal.GoodsList();
            int i = ListGoods.Count();
            return i;
        }
        ///// <summary>
        ///// 快速搜索查询商品数据总条数
        ///// </summary>
        ///// <returns></returns>
        //public int GoodsCount(string GoodsType, decimal MinPrice, decimal MaxPrice, int GoodsMileage, int CarsModel)
        //{
        //    List<Goods> GoodsList = dal.GoodsList();
        //    List<Goods> list = GoodsList.Where(p => p.GoodsType.Contains(GoodsType) && p.GoodsPrice <= MaxPrice && p.GoodsPrice >= MinPrice && p.GoodsMileage <= GoodsMileage && p.CarsModelsID == CarsModel).ToList();
        //    int i = list.Count();
        //    return i;
        //}

        ///// <summary>
        ///// 分页显示
        ///// </summary>
        ///// <param name="PageIndex">当前页</param>
        ///// <param name="PageSize">每条显示数据条数</param>
        ///// <returns></returns>
        //public List<Goods> GoodsList(int PageIndex, int PageSize)
        //{ 
        //    List<Goods> GoodsList = dal.GoodsList();
        //    List<Goods> list = GoodsList.Skip((PageIndex-1) * PageSize).Take(PageSize).ToList();
        //    return list;
        //}
        /// <summary>
        /// 商品显示
        /// </summary>
        /// <returns>所有商品</returns>
        public List<Goods> GoodsList()
        {
          
            return dal.GoodsList();
        }



    }
}
